import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { UtilService } from '@core/services/util.service';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { NzTabPosition } from 'ng-zorro-antd/tabs';
import { Router } from '@angular/router';

@Component({
  selector: 'app-manage-card-edit',
  templateUrl: './manage-card-edit.component.html',
  styles: []
})
export class ManageCardEditComponent implements OnInit {
//房间信息列表
@ViewChild('tree',{static:false}) tree: any;
//用户信息
@Input()type: 0 | 1;
@Input()
set _param(value) {
  console.log(value);
  if (!value) return;
  this.param = value;
};
pIdList:any[]=[];
defaultCheckedKeys:any=[];
searchValue:any="";
param: any;
showForm = false;
checkedList:any[];
houseList:any=[]
isLoading:boolean=true;


//提交楼栋修改数据
formSubmit() {
 let nodes=this.tree.getCheckedNodeList();
 let houseList=""
 if(nodes.length > 0){
    let list= []
    for(let item of nodes){
        list.push(item.key)
    }
    houseList=list.join(",")
 }
 
 this.http.post("/core/door/card/alterManageCard",{id:this.param.id,cardCategory:"3",manageHouse:houseList}).subscribe((res:any)=>{
   if(res.status == "200"){
     this.msg.success("操作成功");
     this.modalRef.triggerOk();
   }else{
     this.msg.error(res.message);
   }
 })
}

resetForm(data){
 this.modalRef.destroy("ok");
}

constructor(
  private api: ApiService,
  private http: HttpClient,
  private msg: NzMessageService,
  private modalRef: NzModalRef,
  private util:UtilService,
  private router: Router,
) { }

nzTabPosition: NzTabPosition = 'left';

ngOnInit() {
  this.showForm = true;
  this.getHouses();
}

selectChage($event){
  //console.log($event);
  //this.router.navigateByUrl("/house-manage");
}

 //获取楼栋房间列表
getHouses(): void{
 this.http.post(this.api.manager.queryUnitHouse.url,{"areaParentAllName":""}).subscribe( (res:any) => {
   //console.log(res);
   if (res.status === '200' && res.data && res.data.list.length > 0) {
     let list=[];
     for(let item of res.data.list){
       list.push({
         key:item.id,
         pId:item.areaId == undefined ? "" : item.areaId,
         title:item.areaParentAllName,
         id:item.id,
         value:item.id
       })
     }
     if(this.param.manageHouse){
      this.defaultCheckedKeys=this.param.manageHouse.split(",")
      let selectedArr=[]
      for(let item of this.defaultCheckedKeys){
        for(let i=0;i<list.length;i++){
          if(list[i].key == item){
            selectedArr.push(list[i])
            list.splice(i,1)
            i--
          }
        }
      }
      this.houseList = [...selectedArr,...list]
     }else{
      this.houseList=list
      this.defaultCheckedKeys=[]
     }

   }
   this.isLoading=false;
 })
}

}
